<template>
  <div class="login-page-back-div">
    <div class="form-div">
      <a-form
        :model="form"
        hide-label="true"
        layout="vertical"
        @submit="handleSubmit"
        :rules="rules"
      >
        <a-form-item field="username" :hide-asterisk="true">
          <a-input v-model="form.username" placeholder="请输入用户名...">
            <!-- 图标 -->
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item field="password" :hide-asterisk="true">
          <a-input-password v-model="form.password" placeholder="请输入密码...">
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item field="isRemember">
          <a-checkbox v-model="form.isRemember"> 记住我 </a-checkbox>
        </a-form-item>

        <div class="login-div">
          <a-form-item>
            <a-button html-type="submit" style="width: 100%" :loading="loading">登陆</a-button>
          </a-form-item>
        </div>
      </a-form>
    </div>
  </div>
</template>

<script setup>
// 什么时候用{} 什么时候不用
import storage from '@/stores/storage'
import { ref } from 'vue'
import { LOGIN } from '@/api/vblog'
import { Message } from '@arco-design/web-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 提交的数据
const form = ref({
  username: '',
  password: '',
  isRemember: false
})

// button 的loading状态
const loading = ref(false)

// 点击登陆时触发
const handleSubmit = async (data) => {
  // 没有errors时
  if (!data.errors) {
    // console.log(data.values)

    try {
      // 设置Loading状态
      // 请求登陆接口
      // 接收返回的数据
      // 跳转后台页面
      loading.value = true
      //   const resp = LOGIN(data.values)
      const resp = await LOGIN(data.values)
      //
      // console.log(resp)

      if (resp.status === 200) {
        // if (data.values.isRemember) {}
        // 持久化存储
        // resp.data
        storage.value.token = resp.data

        Message.success('登陆成功')
        // 跳转后台页面
        router.push({ name: 'BackendPage' })
      }
    } catch (error) {
      // 登陆失败
      // console.log(error)
      Message.error(error.code + ' ' + error.message)
    } finally {
      // 不管是否登陆成功，取消Loading状态
      loading.value = false
    }
  }
}

// 验证
const rules = {
  username: [
    {
      required: true,
      message: '用户名为必填项'
    }
  ],
  password: [
    {
      required: true,
      message: '密码为必填项'
    }
  ]
}
</script>

<style lang="css" scoped>
.login-page-back-div {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.form-div {
  width: 500px;
  height: 300px;
  display: flex;
  align-items: center;
  border: solid 1px var(--color-neutral-4);
  padding: 30px;
}

.login-div {
  width: 100%;
}

/* 覆盖原有样式 */
.login-div :deep(.arco-form-item-content-flex) {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
  width: 100%;
}
</style>
